﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using OA.Manager;
using OA.Model;
using OA.Utility;

namespace OA.Bll.Personnel
{
    public class EmployeeEducationMappingPresenter : BasePresenter<EmployeeEducationMappingInfo>
    {
        private readonly EmployeeEducationMappingManager _manager;

         public EmployeeEducationMappingPresenter()
        {
            _manager = new EmployeeEducationMappingManager();
        }

        public new string Add(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] {';'});
            EmployeeEducationMappingInfo employeeEducation = null;
            int EmployeeId;
            int EducationId;
            bool bl = false;
            foreach (string id in ids)
            {
                int.TryParse(id, out EmployeeId);
                int.TryParse(req.Options["EducationId"], out EducationId);
                employeeEducation = new EmployeeEducationMappingInfo();
                employeeEducation.EmployeeId = EmployeeId;
                employeeEducation.EducationId = EducationId;
                if (
                    !_manager.IsEmployeeEducationMappingInfo(" EmployeeId='" +
                                                             employeeEducation.EmployeeId.ToString(
                                                                 CultureInfo.InvariantCulture) + "' AND  EducationId='" +
                                                             employeeEducation.EducationId.ToString(
                                                                 CultureInfo.InvariantCulture) + "'"))
                {
                    bl = Manager.InsertModelInfo(employeeEducation);
                    if (!bl)
                        break;
                }
                else
                    bl = true;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        }

        public new string Delete(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] {';'});
            bool bl = false;
            foreach (string id in ids)
            {
                bl =
                    _manager.DeleteEmployeeEducationMappingInfo(" EmployeeId='" + id + "' AND EducationId='" +
                                                                req.Options["EducationId"] +
                                                                "'");
                if (!bl)
                    break;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Delete);
        }
        public string AddPersonalWorkEducation(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] {';'});
            EmployeeEducationMappingInfo employeeEducation = null;
            int EmployeeId;
            int EducationId;
            bool bl = false;
            foreach (string id in ids)
            {
                int.TryParse(id, out EducationId);
                int.TryParse(req.Options["EmployeeId"], out EmployeeId);
                employeeEducation = new EmployeeEducationMappingInfo();
                employeeEducation.EmployeeId = EmployeeId;
                employeeEducation.EducationId = EducationId;
                if (
                    !_manager.IsEmployeeEducationMappingInfo(" EmployeeId='" +
                                                             employeeEducation.EmployeeId.ToString(
                                                                 CultureInfo.InvariantCulture) + "' AND  EducationId='" +
                                                             employeeEducation.EducationId.ToString(
                                                                 CultureInfo.InvariantCulture) + "'"))
                {
                    bl = Manager.InsertModelInfo(employeeEducation);
                    if (!bl)
                        break;
                }
                else
                    bl = true;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        }
        public string DeletePersonalWorkEducation(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] { ';' });
            bool bl = false;
            foreach (string id in ids)
            {
                bl =
                    _manager.DeleteEmployeeEducationMappingInfo(" EmployeeId='" + req.Options["EmployeeId"] +
                                                                "' AND EducationId='" +
                                                                id +
                                                                "'");
                if (!bl)
                    break;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Delete);
        }
    }
}
